package org.dei.dataserver;

import java.sql.SQLException;
import java.util.Iterator;

import org.dei.perla.rockfall.RockfallManager;
import org.dei.perla.rockfall.RockfallManagerOptimized;
import org.dei.perla.rockfall.RockfallManagerVanilla;
import org.dei.perla.rockfall.gui.MainWindow;
import org.dei.perla.sys.dbutils.PerlaSQLExec;
import org.dei.perla.sys.device.fpc.FPC;
import org.dei.perla.utils.PerlaEnv;
import org.dei.perla.utils.PerlaRuntime;

public class GuiDSPic {
	
	private static RockfallManager manager;

	public static void main(String args[]) {
		PerlaEnv.init();
		PerlaRuntime.init();
		try {
			PerlaSQLExec.init();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(args.length > 1){
			if(args[1].equals("vanilla"))
				manager = new RockfallManagerVanilla();
			else{
				if(args[1].equals("optimized"))
					manager = new RockfallManagerOptimized();
				else{
					manager = new RockfallManager();

				}
			}
		}
		else{
			manager = new RockfallManager();
		}
		
		// Inizializzo l'interfaccia grafica ed aggancio le varie FPC
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainWindow window = new MainWindow(manager);
				window.showGui();
				
				// Aggiungo tutti i DSPic da cui voglio ricevere i dati
				Iterator<FPC> iterator = manager.getListIterator();
				//int i = 0;
		/*		while (iterator.hasNext()) {
					FPC fpc = iterator.next();
					if(i == 0){
						IntecFpc intecFpc = (IntecFpc) fpc;
						window.addIntecDevice("Intec ", intecFpc.registerQuery(null));
						i++;
					}
					else{
						if (fpc instanceof DSPicFPC) {
							DSPicFPC picFPC = (DSPicFPC) fpc;
							window.addDevice("DSPic " + i++, picFPC.registerQuery(null));					
						}
					}
				}*/
			}
		});
	}
}
